package com.gaswell.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaswell.entity.Qba01;
import com.gaswell.entity.Qba01M;
import com.gaswell.entity.Qfc01;
import com.gaswell.mapper.KSParaDatasMapper;
import com.gaswell.mapper.Qba01MMapper;
import com.gaswell.mapper.RtuDataMapper;
import com.gaswell.pojo.ALLQj;
import com.gaswell.pojo.KSParaDatas;
import com.gaswell.pojo.ReciveCycleData;
import com.gaswell.service.*;
import com.gaswell.utils.DateUtils;
import com.gaswell.utils.StatusDetect;
import com.gaswell.vo.QjVo;
import com.gaswell.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Future;

/**
 * <p>
 * 实时数据 服务实现类
 * </p>
 *
 * @author Lei Wang
 * @since 2022-05-24
 */
@Service
@DS("rtu")
public class KSParaDatasServiceImpl extends ServiceImpl<KSParaDatasMapper, KSParaDatas> implements KSParaDatasService {
    @Autowired
    ALLQjService allQjService;


    @Override
    public Page<KSParaDatas> pageByJhAndTime(Page<KSParaDatas> page, List<String> jhs, String start, String end,int department) {


        LambdaQueryWrapper<KSParaDatas> ksParaDatasLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (jhs.size() > 0) {
            //      根据井号拿到中文井号和英文井号
            List<ALLQj> zwjhAndYwjh = allQjService.findZwjhAndYwjh(jhs);
            LinkedList<String> zwbhs = new LinkedList<>();
            LinkedList<String> ywbhs = new LinkedList<>();
//        将英文井号和中文井号分别存储下来
            for (ALLQj allQj : zwjhAndYwjh) {
                zwbhs.add(allQj.getZwbh());
                ywbhs.add(allQj.getYwbh());
            }

            if (zwbhs.size() != 0)
                ksParaDatasLambdaQueryWrapper.and(l->l.in(KSParaDatas::getWellName, zwbhs).or().in(KSParaDatas::getWellName, ywbhs));
//        查到的井号为0，并且指定了查询编号，返回空值
            if (zwbhs.size() == 0) {
                return page;
            }
        }


        if (start != null && start != "") {
            Timestamp start_time = DateUtils.stringToDate(start);
            ksParaDatasLambdaQueryWrapper.ge(KSParaDatas::getTestTime, start_time);
        }
        if (end != null && end != "") {
            Timestamp end_time = DateUtils.stringToDate(end);
            ksParaDatasLambdaQueryWrapper.le(KSParaDatas::getTestTime, end_time);
        }

        ksParaDatasLambdaQueryWrapper.orderByDesc(KSParaDatas::getTestTime);
        if (department == 2) {
            List<String> ywbhs = allQjService.findYwjh("川渝");
            List<String> zwbhs = allQjService.findZwjh("川渝");
            ksParaDatasLambdaQueryWrapper.and(l -> l.in(KSParaDatas::getWellName, ywbhs).or().in(KSParaDatas::getWellName, zwbhs));
        }
        if (department == 3) {
            List<String> ywbhs = allQjService.findYwjh("大庆");
            List<String> zwbhs = allQjService.findZwjh("大庆");
            ksParaDatasLambdaQueryWrapper.and(l -> l.in(KSParaDatas::getWellName, ywbhs).or().in(KSParaDatas::getWellName, zwbhs));
        }





        Page<KSParaDatas> ksParaDatasPage = baseMapper.selectPage(page, ksParaDatasLambdaQueryWrapper);
        return ksParaDatasPage;
    }
}
